import * as YvanUI from "yvan-ui-wotu"
import CustomDict from '../widget/custom.dict'
import * as CustomService from '@server/example/CustomService'

const React = YvanUI.React
const _ = YvanUI.lodash

interface Ref {
    grid1: YvanUI.Grid
}

export default class GridSelected extends YvanUI.Scope<void, Ref> {

    main = this.$$({
        selectedRow: null
    })

    vjson = this.$$({
        view: 'page',
        title: '表格选中',
        items: [
            {
                view: 'card',
                bodyStyle: {
                    padding: 0
                },
                items: [
                    {
                        view: 'grid',
                        title: '表格选中',
                        ref: 'grid1',
                        bindSelected: 'main.selectedRow',
                        idField: "customerid",
                        toolbar: [
                            {
                                view: 'button',
                                text: '刷新',
                                icon: 'fa fa-refresh',
                                type: 'primary',
                                onClick: () => {
                                    this.refs.grid1.refresh()
                                }
                            },
                        ],
                        columns: [
                            {field: "customerid", hidden: true},
                            {field: "customer_code", title: "客户编号", width: 80,},
                            {field: "customer_name", title: "客户名称", width: 120},
                            {field: "logogram", title: "助记码", width: 80},
                            {field: "industry_class", title: "客户类型", width: 100, data: CustomDict.industryclass},
                            {field: "customer_type", title: "客户类别", width: 100, data: CustomDict.customertype},
                            {field: "accounts", title: '结算方'},
                            {field: "shipper", title: '承运方'},
                            {field: "zip", title: '邮编'},
                            {field: "contact", title: '联系人'},
                            {field: "telephone", title: '电话'},
                            {field: "is_enable", title: '是否启用', formatter: (v) => (_.trim(v) === '1' ? '启用' : '不启用')},
                            {field: "remark", title: '备注'},
                            {field: "create_by", title: '创建者'},
                            {field: "create_at", title: '建立时间', formatter: 'fmtDate'},
                            {field: "update_by", title: '修改者'},
                            {field: "update_at", title: '更新时间', formatter: 'fmtDate'},
                        ],
                        autoDataSource: true,
                        dataSource: {
                            func: CustomService.selectAll
                        }
                    },
                    {
                        view: 'card',
                        items: [
                            () => (
                                <div>
                                    <pre>{JSON.stringify(this.main.selectedRow)}</pre>
                                </div>
                            )
                        ]
                    }
                ]
            }
        ]
    })
}